﻿using System;
using MapWindow.Tools.Param;
using System.Windows.Forms;
using System.IO;
using MapWindow.Tools.Toolbox;
using System.Collections.Generic;

namespace MapWindow.Tools.DialogComponents
{
    public class SaveFileElement : DialogElement
    {
        private DataSetArray _addedTextFile;
        private Boolean _refreshCombo = true;
        private string _fileName;
        private List<DataSetArray> _dataSets;

        
        public SaveFileElement(FileParam param, string text)
        {
            InitializeComponent();
            base.Param = param;
            _fileName = text;
            // textBox1.Text = param.Value;
            GroupBox.Text = param.Name;
            Refresh();

            //Populates the dialog with the default parameter value
            if (param.Value != null && param.DefaultSpecified == true)
            {
                _fileName = param.ModelName;
                base.Status = ToolStatus.Ok;
                base.LightTipText = MapWindow.MessageStrings.FeaturesetValid;
            }
        }

        /// <summary>
        /// Creates an instance of the dialog
        /// </summary>
        /// <param name="outputParam">The parameter this element represents</param>
        /// <param name="dataSets">An array of available data</param>
        public SaveFileElement(FileParam outputParam, List<DataSetArray> dataSets)
        {
            //Needed by the designer
            InitializeComponent();

            //We save the parameters passed in 
            base.Param = outputParam;

            _dataSets = dataSets;

            //Saves the label
            this.GroupBox.Text = Param.Name;

            Refresh();
        }



        /// <summary>
        /// updates the param if something's been changed
        /// </summary>
        public override void Refresh()
        {
            //Disable the combo box temporarily
            _refreshCombo = false;

            //We set the combo boxes status to empty to start
            base.Status = ToolStatus.Empty;
            base.LightTipText = MapWindow.MessageStrings.FeaturesetMissing;
            txtDataTable.Text = "";

            //If the user added a text file
            if (_addedTextFile != null)
            {
                txtDataTable.Text = _addedTextFile.Name;
                if (base.Param.Value != null && base.Param.DefaultSpecified == true)
                {
                    if (_addedTextFile.DataSet == base.Param.Value)
                    {
                        txtDataTable.Text = _addedTextFile.Name;
                        base.Status = ToolStatus.Ok;
                        base.LightTipText = MapWindow.MessageStrings.FeaturesetValid;
                    }
                }
            }

            //Add all the dataSets back to the combo box
            if (_dataSets != null)
            {
                foreach (DataSetArray dsa in _dataSets)
                {
                    TextFile aTextFile = (dsa.DataSet as TextFile);
                    if (aTextFile != null)
                    {
                        //If the featureset is the correct type and isn't already in the combo box we add it
                        if (txtDataTable.Text != dsa.Name)
                        {
                            //txtDataTable.Text = dsa.Name;
                            if (base.Param.Value != null && base.Param.DefaultSpecified == true)
                            {
                                if (dsa.DataSet == base.Param.Value)
                                {
                                    txtDataTable.Text = dsa.Name;
                                    base.Status = ToolStatus.Ok;
                                    base.LightTipText = MapWindow.MessageStrings.FeaturesetValid;
                                }
                            }
                        }
                    }
                }
            }

            _refreshCombo = true;
        }



        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.txtDataTable = new System.Windows.Forms.TextBox();
            this.btnAddData = new System.Windows.Forms.Button();
            this._groupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // _groupBox1
            // 
            this._groupBox1.Controls.Add(this.btnAddData);
            this._groupBox1.Controls.SetChildIndex(this.btnAddData, 0);
            // 
            // txtDataTable
            // 
            this.txtDataTable.Location = new System.Drawing.Point(45, 13);
            this.txtDataTable.Name = "txtDataTable";
            this.txtDataTable.Size = new System.Drawing.Size(356, 20);
            this.txtDataTable.TabIndex = 9;
            // 
            // btnAddData
            // 
            this.btnAddData.Image = global::MapWindow.Images.AddLayer;
            this.btnAddData.Location = new System.Drawing.Point(432, 10);
            this.btnAddData.Name = "btnAddData";
            this.btnAddData.Size = new System.Drawing.Size(26, 26);
            this.btnAddData.TabIndex = 8;
            this.btnAddData.UseVisualStyleBackColor = true;
            this.btnAddData.Click += new System.EventHandler(this.btnAddData_Click);
            // 
            // SaveFileElement
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Controls.Add(this.txtDataTable);
            this.Name = "SaveFileElement";
            this.Controls.SetChildIndex(this._groupBox1, 0);
            this.Controls.SetChildIndex(this.txtDataTable, 0);
            this._groupBox1.ResumeLayout(false);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox txtDataTable;
        private System.Windows.Forms.Button btnAddData;

        private void btnAddData_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
            sfd.OverwritePrompt = true;
            sfd.Filter = "csv files|*.csv";
            if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                TextFile tmpTextFile = new TextFile(sfd.FileName);
                _addedTextFile = new DataSetArray(Path.GetFileNameWithoutExtension(sfd.FileName), tmpTextFile);

                base.Param.ModelName = _addedTextFile.Name;
                base.Param.Value = _addedTextFile.DataSet as object;
                
                Refresh();
                
                base.Status = ToolStatus.Ok;
                base.LightTipText = MapWindow.MessageStrings.FeaturesetValid;
                
                //If the text file is null do nothing the user probably hit cancel
                if (_addedTextFile == null)
                {
                    return;
                }
                else
                {
                    txtDataTable.Text = _addedTextFile.Name;
                }
            }
        }

    }
}
